<?php
/**
 * 库存总表（sku）
 * (model)
 */

namespace App\Models\Inventory;

use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class InventoryTotalSku extends Model{

    protected $table = 'inventory_total_sku';

    protected $guarded = [];

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['classify_title'] = $row->sku->classify->title ?? '';

            $temp['cn_instant'] = $row->cn_instant ?? 0;
            $temp['cn_transit'] = $row->cn_transit ?? 0;
            $temp['cn_reserve'] = $row->cn_reserve ?? 0;

            $temp['fba_instant'] = $row->fba_instant ?? 0;
            $temp['fba_transit'] = $row->fba_transit ?? 0;

            $temp['os_transit'] = ($row->os_box_instant ?? 0) + ($row->os_removale_instant ?? 0) + ($row->os_dropshipping_instant ?? 0);
            $temp['os_transit'] = ($row->os_box_transit ?? 0) + ($row->os_removale_transit ?? 0) + ($row->os_dropshipping_transit ?? 0);
            $temp['os_reserve'] = ($row->os_box_reserve ?? 0) + ($row->os_removale_reserve ?? 0) + ($row->os_dropshipping_reserve ?? 0);

            $temp['os_box_instant'] = $row->os_box_instant ?? 0;
            $temp['os_box_transit'] = $row->os_box_transit ?? 0;
            $temp['os_box_reserve'] = $row->os_box_reserve ?? 0;

            $temp['os_removale_instant'] = $row->os_removale_instant ?? 0;
            $temp['os_removale_transit'] = $row->os_removale_transit ?? 0;
            $temp['os_removale_reserve'] = $row->os_removale_reserve ?? 0;

            $temp['os_dropshipping_instant'] = $row->os_dropshipping_instant ?? 0;
            $temp['os_dropshipping_transit'] = $row->os_dropshipping_transit ?? 0;
            $temp['os_dropshipping_reserve'] = $row->os_dropshipping_reserve ?? 0;

            yield $temp;
        }
    }

}
